/**
 * Created by cchen on 2015/6/8.
 */
'use strict';

angular.module('backofficeApp.questionAdd.controllers', []).
    controller('questionAddCtrl', ['$scope', 'MasterDataService', 'CommunityService', '$http', '$location', function ($scope, masterDataService, communityService, $http, $location) {
        $scope.communityLabelList = communityService.getAllCommunityService();
        $scope.tagList = masterDataService.getAllDicService({category: '02'});

        $scope.categoryList = [
            {
                name: '邻里求助',
                value: 'question'
            },
            {
                name: '推荐分享',
                value: 'share'
            },
            {
                name: '二手闲置',
                value: 'secondhand'
            },
            {
                name: '免费赠送',
                value: 'free'
            },
            {
                name: '失物招领',
                value: 'lostAndFound'
            },
            {
                name: '邻里广告',
                value: 'advertisement'
            }
        ];

        //小区多选框
        $scope.communityIds = [];
        var communityStr = '';
        $scope.toggle = function (ck, objId) {
            if (communityStr.indexOf(objId) != -1) {
                ck = true;
            }
            !ck ? $scope.communityIds.push(objId) : $scope.communityIds.remove(objId);

            communityStr = '';
            $scope.communityIds.forEach(function (community) {
                communityStr += community
            });
        };

        $scope.allCheck = function (ck) {
            if (!ck) {
                $scope.communityIds = [];
                communityStr = '';
                $("[name = chkItem]:checkbox").prop("checked", true);
                $scope.communityLabelList.data.forEach(function (community) {
                    $scope.communityIds.push(community.objId);
                    communityStr += community.objId
                })
            } else {
                $scope.communityIds = [];
                $("[name = chkItem]:checkbox").prop("checked", false);
                communityStr = '';
            }
        };


        $scope.tag = new Array();
        $scope.tagToggle = function (ck, objId) {
            !ck ? $scope.tag.push(objId) : $scope.tag.remove(objId);
        };


        $scope.gotoQuestionListPage = function () {
            $location.path('/recommendQuestion/all/all/all/all/empty/empty/empty/empty/all')
        };

        $scope.selectFile = function (file) {
            document.getElementById("image_result").innerHTML = '';
            showImages();
        };

        var reader = null;
        if (FileReader) {
            reader = new FileReader();
        } else {
            alert("浏览器暂时不支持FileReader");
        }
        var imageEle = document.getElementById("images_upload");
        var fileList = {}, length = 0, length1 = 0;


        var showImages = function () {

            //获取上传的多个文件
            fileList = imageEle.files;
            length = fileList.length;
            length1 = fileList.length;

            //获取文件信息
            readURL();
        }


        var readURL = function () {
            reader.readAsDataURL(fileList[length1 - length]);
            reader.onload = function () {
                var img = document.createElement("img");
                img.src = reader.result;
                img.width = 200;
                document.getElementById("image_result").appendChild(img);
                length--
                if (length == 0)return;
                readURL();
            };
        }


        $scope.createQuestion = function (question) {
            if ($scope.communityIds.length == 0) {
                alert('请选择小区');
                return;
            }
            if (question == null || question.category == null || question.category == '') {
                alert('请选择类别');
                return;
            }
            if (question.desc == null || question.desc == '') {
                alert('请输入描述');
                return;
            }
            if (fileList.length > 6) {
                alert('图片最多传6张');
                return;
            }

            bootbox.confirm('是否确定添加?', function (result) {
                if (result) {
                    var fd = new FormData();
                    if (fileList != null) {
                        for (var i = 0; i < fileList.length; i++) {
                            var index = i + 1;
                            fd.append('image' + index, fileList[i]);
                        }
                    }
                    fd.append('desc', question.desc);
                    fd.append('category', question.category);
                    fd.append('tag', $scope.tag);
                    fd.append('community', $scope.communityIds);


                    $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/question/backPostQuestion', fd, {
                        transformRequest: angular.identity,
                        headers: {'Content-Type': undefined}
                    }).success(function (data) {
                        $location.path('/recommendQuestion/all/all/all/all/empty/empty/empty/empty/all')
                    }).error(function () {
                    });
                }
            }).find("div.modal-dialog").css({width: '30%'});
        };
    }]);